layui.use(
	['table', 'util', 'layer', 'jquery', 'form', 'upload'],
	function () {

		var table = layui.table;
		var layer = layui.layer;
		var form = layui.form;
		var $ = layui.jquery;
		var upload = layui.upload;

		//普通图片上传
		var uploadInst = upload.render({
			elem: '#test1',
			url: 'upload',
			before: function (obj) {
				//预读本地文件示例，不支持ie8
				obj.preview(function (index, file, result) {
					$('#demo1').attr('src', result); //图片链接（base64）
					$("input[name='uimges']").attr("value", result);
				});
			},
			done: function (res) {
				//如果上传失败
				if (res.code > 0) {
					return layer.msg('上传失败');
				}
				//上传成功
			},
			error: function () {
				//演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function () {
					uploadInst.upload();
				});
			}
		});

		/**
		 * 点击个人信息管理的事件
		 */
		$(".active").click(function () {
			$("#qita").attr("style", "display:none;");
			$("#gerenxinxi").attr("style", "display:block;");
			$("#addContents").attr("style", "display:none;");
			$("#gerenxiangqing").attr("style", "display:none;");
			$("#flTitle").text("个人信息管理");
		});
		/**
		 * 点击修改个人信息的事件
		 */
		$(".layui-btn-primary").click(function () {
			$("#qita").attr("style", "display:none;");
			$("#gerenxinxi").attr("style", "display:none;");
			$("#addContents").attr("style", "display:none;");
			$("#gerenxiangqing").attr("style", "display:block;");

		});
		/**
		 * 点击添加按钮的事件
		 */
		$("#layui-btn").click(function () {
			$("#qita").attr("style", "display:none;");
			$("#gerenxinxi").attr("style", "display:none;");
			$("#addContents").attr("style", "display:block;");
			$("#gerenxiangqing").attr("style", "display:none;");
			$("#wenzhang").attr("lay-filter", "loginForm");
			$("#wenzhang").text("文章提交");
			$("input[name='tcontentheader']").val("");
			$("input[name='author']").val("");
			$(".w-e-text").text("");
		});

		$('.page')
			.click(
				function () {
					var cid = $(this).attr('data-cid');
					var cName = $(this).text();
					layer.msg(cName);
					$.post("table", "cid=" + cid, function (data) {
						if (data.data != null) {
							$("#qita").attr("style", "display:block;");
							$("#gerenxinxi").attr("style", "display:none;");
							$("#gerenxiangqing").attr("style", "display:none;");
							$("#addContents").attr("style", "display:none");
							$("#flTitle").text(cName);
							// 第一个实例
							table.render({
								elem: '#demo',
								url: 'table?cid=' + cid // 数据接口,
								,page: true // 开启分页
								, cols: [
									[ // 表头
										{
											field: 'tid',
											title: 'ID',
											width: 80,
											sort: true,
											fixed: 'left'
										}, {
											field: 'tcontentheader',
											title: '标题'
										}, {
											field: 'author',
											title: '作者',
											align:'center'
										}, {
											field: 'tcreationtime',
											title: '创建时间',
											width: 140,
											templet: '<div >{{layui.util.toDateString(d.tcreationtime, "yyyy-MM-dd")}} </div>'
										}, {
											field : 'commentCount',
											title	:"评论数",
											align:'center'
										},{
											fixed: 'right',
											width: 178,
											align: 'center',
											title: '操作',
											toolbar: '#barDemo'
										}
									]
								]
							});
						} else if (data.data == null) {
							$("#qita").attr("style", "display:block;");
							$("#gerenxinxi").attr("style", "display:none;");
							$("#gerenxiangqing").attr("style", "display:none;");
							$("#addContents").attr("style", "display:none");
							$("#flTitle").text(cName);
							table.render({
								elem: '#demo',
								url: 'table?cid=' + cid // 数据接口,
								,page: true // 开启分页
								, cols: [
									[ // 表头
										{
											field: 'tid',
											title: 'ID',
											width: 80,
											sort: true,
											fixed: 'left'
										}, {
											field: 'tcontentheader',
											title: '标题'
										}, {
											field: 'author',
											title: '作者',
											edit: 'text'
										}, {
											field: 'tcreationtime',
											title: '创建时间',
											width: 140,
											templet: '<div >{{layui.util.toDateString(d.tcreationtime, "yyyy-MM-dd")}} </div>'
										}, {
											field : 'commentCount',
											title	:"评论数"
										},{
											fixed: 'right',
											width: 178,
											align: 'center',
											title: '操作',
											toolbar: '#barDemo'
										}
									]
								]
							});
						}
					}, "json");
				})

		// 添加提交
		form.on('submit(loginForm)', function (data) {
			layer.msg(JSON.stringify(data.field));//此处显示输入内容
			$.post('addContent', data.field, function (resp) {
				if (resp) {
					layer.msg('添加成功', {
						icon: 1,
						time: 1000
					}, function () {
						location.href = 'indexs';
					});
				} else {
					layer.msg('业务繁忙');
				}
			}, 'json');

			return false;
		});
		
		  /**
		   * 判断登陆名是否被注册
		   */
		$("input[name='userCode']").blur(function() {
			var userCode=$(this).val();
			$.post("queryByUserCode","userCode="+userCode,function(resp){
				if(resp){
					$(".userCodez").text("此登录名已被注册");
				}else{
					$(".userCodez").text("可以使用");
				}
			});
		})
		
		/**
		 * 判断用户名是否被注册
		 */
		$("input[name='userName']").blur(function() {
			var userName=$(this).val();
			$.post("queryByUserName","userName="+userName,function(resp){
				if(resp){
					$(".userNamez").text("此用户名已被注册");
				}else{
					$(".userNamez").text("可以使用");
				}
			});
		})
		
		
		
		// 修改用户提交
		form.on('submit(userForm)', function (data) {
			var password=data.field.password;
			var passwordz=data.field.passwordz;
			var newpassword=data.field.newpassword;
			var querenpassword=data.field.querenpassword;
			if(password!=passwordz){
				layer.msg("原始密码错误", {
					icon: 2,
					time: 1000
				});
				return false;
			}
			if(newpassword.length<6||newpassword.length>12){
				layer.msg("新密码不能小于6位数或大于12位数", {
					icon: 2,
					time: 1000
				});
				return false;
			}
			if(querenpassword.length<6||querenpassword.length>12){
				layer.msg("确认密码不能小于6位数或大于12位数", {
					icon: 2,
					time: 1000
				});
				return false;
			}
			if(querenpassword!=newpassword){
				layer.msg("两次密码不一致", {
					icon: 2,
					time: 1000
				});
				return false;
			}
			data.field.password=querenpassword;
			$.post('updateUser', data.field, function (resp) {
				if (resp) {
					layer.msg('修改成功', {
						icon: 1,
						time: 1000
					}, function () {
						location.href = 'indexs';
					});
				} else {
					layer.msg('业务繁忙');
				}
			}, 'json');

			return false;
		});


		// 修改内容提交
		form.on('submit(updateForm)', function (data) {
			layer.msg(JSON.stringify(data.field));//此处显示输入内容
			$.post('updateContent', data.field, function (resp) {
				if (resp) {
					layer.msg('修改成功', {
						icon: 1,
						time: 1000
					}, function () {
						location.href = 'indexs';
					});
				} else {
					layer.msg('业务繁忙');
				}
			}, 'json');
			return false;
		});

		// 监听工具条
		table.on('tool(test)', function (obj) {
			var data = obj.data;
			if (obj.event === 'detail') {
				layer.msg('ID：' + data.tid + ' 的查看操作');
			} else if (obj.event === 'del') {
				layer.confirm('真的删除行么', function (index) {
					$.ajax({
						url: 'deleteContent/' + data.tid,
						type: 'DELETE',
						// contentType:'applicaton/json',
						dataType: 'json',
						success: function (resp) {
							if (resp) {
								layer.msg('删除成功');
								location.href = 'indexs';
							}
						}

					});

					layer.close(index);

				});
			} else if (obj.event === 'edit') {
				$.ajax({
					url: 'queryBytid/' + data.tid,
					type: 'post',
					// contentType:'applicaton/json',
					dataType: 'json',
					success: function (resp) {
						if (resp != null) {
							$("#qita").attr("style",
								"display:none;");
							$("#gerenxinxi").attr("style",
								"display:none;");
							$("#addContents").attr("style",
								"display:block;");
							$("#gerenxiangqing").attr("style", "display:none;");
							$("#wenzhang").attr("lay-filter", "updateForm");
							$("#wenzhang").text("文章修改");
							$("input[name='tcontentheader']").val(resp.tcontentheader);
							$("input[name='author']").val(resp.author);
							$(".w-e-text").html(resp.tcontent);
							$("#text1").html(resp.tcontent);
							$("input[name='tid']").val(resp.tid);
							var fenlei = $("#selectType option").length;
							for (var i = 1; i <= fenlei; i++) {
								if (resp.classification.cid == i) {
									$("#selectType").find("option:contains('" + resp.classification.cName + "')").attr("selected", "selected");
									form.render();
									layer.msg(i, {
										icon: 1,
										time: 1000
									});
									break;
								}
							}
						}
					}
				});

			}
		});
		var passwordz=$("input[name='passwordz']").val();
		var newpassword=$("input[name='newpassword']").val();
		var querenpassword=$("input[name='querenpassword']").val();
		form.on('switch(switchTest)', function(data){
			var queren= this.checked ? 'true' : 'false';
			if(queren=="true"){
				$("#passwordz").attr("style","display:block");
				$("#newpassword").attr("style","display:block");
				$("#querenpassword").attr("style","display:sblock");
				$("input[name='passwordz']").val("");
				$("input[name='newpassword']").val("");
				$("input[name='querenpassword']").val("");
			}else{
				$("#passwordz").attr("style","display:none");
				$("#newpassword").attr("style","display:none");
				$("#querenpassword").attr("style","display:none");
				$("input[name='passwordz']").val(passwordz);
				$("input[name='newpassword']").val(newpassword);
				$("input[name='querenpassword']").val(querenpassword);
			}
		  });
	})
